package com.woniuxy.hrms.util;

import javax.xml.transform.Result;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @program: hrms
 * @description:
 * @create: 2024-08-08 17:10
 **/
public class DBUtil {
    static Properties properties = new Properties();

    static {
        InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("config.properties");
        //加载配置文件
        try {
            properties.load(inputStream);
            String driver = properties.getProperty("jdbc.driver");//读取配置文件中的内容
            Class.forName(driver);//加载驱动
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //得到连接对象
    public static Connection getCon() throws SQLException {
        String url = properties.getProperty("jdbc.url");
        String username = properties.getProperty("jdbc.username");
        String password = properties.getProperty("jdbc.password");

        return DriverManager.getConnection(url, username, password);
    }

    //关闭相关对象
    public static void release(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}
